package com.friend.chat.utils;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.friend.chat.R;


/**
 * @author gorilla
 * @des
 * @date 2021/4/15
 */
public class GlideUtils {

    public static void loadImg(ImageView imageView, String url) {
        if (imageView == null) {
            return;
        }
        RequestOptions options = new RequestOptions();
       /* options.placeholder(R.drawable.d_defualt_palceholder)
                .error(R.drawable.d_defualt_palceholder);*/


        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .into(imageView);

    }

    /**
     * 加载头像
     *
     * @param imageView
     * @param url
     */
    public static void loadHeaderImg(ImageView imageView, String url) {
        if (imageView == null) {
            return;
        }
        RequestOptions options = new RequestOptions();
        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .into(imageView);
    }

    /**
     * 圆角
     */
    @Deprecated
    public static void loadRadiusImg(ImageView imageView, String url, int dpValue) {
        if (imageView == null) {
            return;
        }
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(dp2px(imageView.getContext(), dpValue));
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .into(imageView);
    }


    public static void loadRadiusImg1(ImageView imageView, String url, int dpValue) {
        if (imageView == null) {
            return;
        }
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(dp2px(imageView.getContext(), dpValue));
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);

        Glide.with(imageView.getContext())
                .load(url)
                .transform(new GlideRoundTransform(imageView.getContext(), 5))
                // 内存缓存
                .skipMemoryCache(false)
                // 磁盘不缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                 .apply(options)
                .into(imageView);
    }

    public static int dp2px(Context ctx, float dp) {
        float density = ctx.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }


}
